<?php

class Country extends DataMapper {

    var $table = 'countries';

    var $has_many = array(
    	'user_profile' => array(			
            'class' => 'user_profile',
            'other_field' => 'country',
            'join_self_as' => 'country',
            'join_other_as' => 'profile',
            'join_table' => 'r_countries_profiles'),
    );
    
    var $has_one = array(
        	'zone' => array(			
                'class' => 'zone',
                'other_field' => 'country',
                'join_self_as' => 'country',
                'join_other_as' => 'zone',
                'join_table' => 'r_countries_zones'),
    );

    var $validation = array(
       'code' => array(
            'label' => 'Country code',
            'rules' => array('required', 'trim', 'unique', 'alpha_dash', 'min_length' => 2, 'max_length' => 2),
		)
    );
    
	static function get_array()
    {
    	$ci =& get_instance();
    	$ci->db->select("id, ".$ci->session->userdata('uilang')." as label");
    	$ci->db->order_by('label', 'ASC');
    	$q = $ci->db->get('countries');

    	if($q->num_rows() > 0 )  {
    		$res = array();
    		foreach($q->result() as $row) {
    			$res[$row->id] = $row->label;
    		}
    		return $res;
    	}
    	return NULL;
    }
    
	function get_country_by_id($country_id = 0)
    {
    	$this->db->where('id', $country_id);
    	$query = $this->db->get($this->table);
    	
    	if ($query->num_rows() == 1) return $query->row();
    	return NULL;
    }
    
    function get_country_by_iso_code($iso_code = '')
    {
    	$this->db->where('code', $iso_code);
    	$query = $this->db->get($this->table);
    	 
    	if ($query->num_rows() == 1) return $query->row();
    	return NULL;
    }
}

/* End of file country.php */
/* Location: ./application/models/country.php */